/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package mx.edu.itschapala.sistemas.septimo.Vista;

import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;
import mx.edu.itschapala.sistemas.septimo.Control.ProductoLnLocal;
import mx.edu.itschapala.sistemas.septimo.Control.ClienteLnLocal;
import mx.edu.itschapala.sistemas.septimo.Modelo.Cliente;
import mx.edu.itschapala.sistemas.septimo.Modelo.Producto;
import mx.edu.itschapala.sistemas.septimo.Vista.enumeracion.TipoBusqueda;
import mx.edu.itschapala.sistemas.septimo.Vista.enumeracion.TipoVentana;

/**
 *
 * @author Sony
 */
public class ClienteListar extends javax.swing.JFrame {
 private final ClienteLnLocal infoCliente;
 private DefaultTableModel modelo;
 private List<Cliente> listac;
 private JFrame ventana;
    /**
     * Creates new form ClienteListar
     */
  public ClienteListar(ClienteLnLocal infoCliente) {
      this.infoCliente=infoCliente;
      initComponents();
      String nombres[] = {"Nombre","Sexo","Id","Telefono","Direccion"};
        modelo = new DefaultTableModel(nombres, 0){
            public boolean isCellEditable(int row, int column){
               return false; 
            }
    };
jTable2.setModel(modelo);
//////////////////////////CLIENTE//////////////////////////////////////    
    listac=infoCliente.ClientegetTodos();
    for (Cliente c : listac){
        
        Object renglonc []=new Object[5];
        renglonc[0]=c.getNombre();
        renglonc[1]=c.getSexo();
        renglonc[2]=c.getId() + "";
        renglonc[3]=c.getTelefono() + "";
        renglonc[4]=c.getDireccion();
        modelo.addRow(renglonc);
          } 
       } 

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jTextField1 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        Registrar = new javax.swing.JButton();
        Actualizar = new javax.swing.JButton();
        RegresarAlMenu = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        jButton1.setText("jButton1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(204, 204, 255));
        jPanel1.setForeground(new java.awt.Color(204, 204, 255));

        jRadioButton1.setText("Contiene");

        jRadioButton2.setText("Inicia");

        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField1KeyPressed(evt);
            }
        });

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable2MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable2);

        Registrar.setBackground(new java.awt.Color(51, 102, 255));
        Registrar.setForeground(new java.awt.Color(255, 255, 255));
        Registrar.setText("Registrar");
        Registrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RegistrarActionPerformed(evt);
            }
        });

        Actualizar.setBackground(new java.awt.Color(51, 102, 255));
        Actualizar.setForeground(new java.awt.Color(255, 255, 255));
        Actualizar.setText("Actualizar");
        Actualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ActualizarActionPerformed(evt);
            }
        });

        RegresarAlMenu.setBackground(new java.awt.Color(51, 102, 255));
        RegresarAlMenu.setForeground(new java.awt.Color(255, 255, 255));
        RegresarAlMenu.setText("Regresar Al Menu");
        RegresarAlMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RegresarAlMenuActionPerformed(evt);
            }
        });

        jLabel1.setIcon(new javax.swing.ImageIcon("C:\\Users\\Sony\\Pictures\\Fotos proyecto\\Listar.jpg")); // NOI18N
        jLabel1.setText("jLabel1");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(51, 51, 51)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jRadioButton2)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jRadioButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 319, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(Registrar)
                        .addGap(18, 18, 18)
                        .addComponent(Actualizar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(RegresarAlMenu)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jRadioButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButton2)
                .addGap(4, 4, 4)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Registrar)
                    .addComponent(Actualizar)
                    .addComponent(RegresarAlMenu))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 2, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyPressed

        List<Cliente> tmpc= new ArrayList();
        if(jRadioButton1.isSelected()){
            tmpc=infoCliente.ClientegetPornombre(jTextField1.getText()+evt.getKeyChar(), TipoBusqueda.CONTIENE);
        }else{
            tmpc= infoCliente.ClientegetPornombre(jTextField1.getText()+evt.getKeyChar() , TipoBusqueda.INICIA);
        }
        String nombres[]= {"Nombre","Sexo","Id","Telefono","Direccion"};
        modelo = new DefaultTableModel(nombres, 0){
            public boolean isCellEditable(int row, int column){
                return false;
            }
         };
        
        for (Cliente c : tmpc){
            String renglonc []=new String [5];
            renglonc[0]=c.getNombre();
            renglonc[1]=c.getSexo();
            renglonc[2]=c.getId()+"";
            renglonc[3]=c.getTelefono()+"";
            renglonc[4]=c.getDireccion();
            modelo.addRow(renglonc);
        }
        jTable2.setModel(modelo);
        listac=tmpc;
                                        
   
    }//GEN-LAST:event_jTextField1KeyPressed

    private void jTable2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable2MouseClicked
       if(evt.getClickCount()==2){
            //System.out.println(jTable2.getSelectedRow());
            Cliente c =listac.get(jTable2.getSelectedRow());
         
            
            if(ventana==null){
                ventana=new DialogoCliente(c,infoCliente);
            }else{
                ventana.dispose();
                ventana=new DialogoCliente(c,infoCliente);
                }
            ventana.setVisible(true);
            
                      }  
                                       

    }//GEN-LAST:event_jTable2MouseClicked

    private void RegresarAlMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RegresarAlMenuActionPerformed
        this.dispose();
    }//GEN-LAST:event_RegresarAlMenuActionPerformed

    private void ActualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ActualizarActionPerformed
        String nombres[]={"Nombre","Sexo","Id","Telefono","Direccion"};
        modelo=new DefaultTableModel(nombres,0){
            public boolean isCellEditable(int row, int column){
                return false;
            }
        };
        
        
         jTable2.setModel(modelo);
         listac=infoCliente.ClientegetTodos();
         for(Cliente c: listac){
             String renglonc[]= new String[5];
             renglonc[0]=c.getNombre();
             renglonc[1]=c.getSexo();
             renglonc[2]=c.getId()+"";
             renglonc[3]=c.getTelefono()+"";
             renglonc[4]=c.getDireccion();
             modelo.addRow(renglonc);
         }
             
             
    }//GEN-LAST:event_ActualizarActionPerformed

    private void RegistrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RegistrarActionPerformed
        new ClienteCrearEditar(null, infoCliente,TipoVentana.VENTANA_REGISTRO).setVisible(true);
    }//GEN-LAST:event_RegistrarActionPerformed

     
        
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Actualizar;
    private javax.swing.JButton Registrar;
    private javax.swing.JButton RegresarAlMenu;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
